<?php

use Drupal\shusy_vip\Entity\ShusyVip;
use Drupal\shusy_vip\Entity\ShusyVipBind;

function shusy_vip_shusy_vip_entity_form($form, &$form_state, ShusyVip $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_vip') . '/shusy_vip.forms.inc';
  $form_state[ShusyVip::class] = $e;

  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('VIP名'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['rebate'] = [
    '#type' => 'textfield',
    '#title' => t('比例折扣'),
    '#required' => TRUE,
    '#default_value' => $e->rebate / 100,
    '#description' => t('保留2位小数'),
    '#field_suffix' => t('%'),
    '#maxlength' => 8,
    '#size' => 8,
    '#element_validate' => ['element_validate_number'],
  ];
  $form['amount'] = [
    '#type' => 'textfield',
    '#title' => t('消费累计'),
    '#required' => TRUE,
    '#default_value' => $e->amount / 100,
    '#maxlength' => 8,
    '#size' => 8,
    '#element_validate' => ['element_validate_number'],
  ];
  $form['sort'] = [
    '#title' => '排序',
    '#type' => 'textfield',
    '#required' => TRUE,
    '#maxlength' => 8,
    '#size' => 8,
    '#element_validate' => ['element_validate_number'],
    '#default_value' => $e->sort,
    '#description' => t('数字越小 越靠上'),
  ];
  $form['is_default'] = [
    '#type' => 'checkbox',
    '#title' => t('默认'),
    '#default_value' => $e->is_default,
    '#description' => t('申请即成为此级别的VIP.默认只能有一个,设置了此默认其它的自动取消.'),
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_vip_shusy_vip_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyVip::class];
  if ($e instanceof ShusyVip) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->rebate = ceil($e->rebate * 100);
    $e->amount = ceil($e->amount * 100);
    $e->save();
  }
}

function shusy_vip_shusy_vip_delete_form($form, &$form_state, ShusyVip $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_vip') . '/shusy_vip.forms.inc';
  $form_state[ShusyVip::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_vip/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_vip_shusy_vip_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyVip::class];
  if ($e instanceof ShusyVip) {
    $e->delete();
  }
}

function shusy_vip_shusy_vip_bind_entity_form($form, &$form_state, ShusyVipBind $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_vip') . '/shusy_vip.forms.inc';
  $form_state[ShusyVipBind::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_vip_shusy_vip_bind_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyVipBind::class];
  if ($e instanceof ShusyVipBind) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_vip_shusy_vip_bind_delete_form($form, &$form_state, ShusyVipBind $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_vip') . '/shusy_vip.forms.inc';
  $form_state[ShusyVipBind::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_vip_bind/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_vip_shusy_vip_bind_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyVipBind::class];
  if ($e instanceof ShusyVipBind) {
    $e->delete();
  }
}

